export class TimeTrigger {
	constructor() {
		this.list = [];
		this.lastTimestamp = 0;
	}

	next(message, delay = 0) {
		const timestamp = delay + Date.now();
		if (timestamp < this.lastTimestamp) return;

		this.lastTimestamp = timestamp;

		this.id = setTimeout(() => {
			if (this.lastTimestamp !== timestamp) return;

			this.list.forEach((func) => {
				func(message);
			});
		}, delay);
	}

	subscribe(func) {
		this.list.push(func);
	}
}

export default {};
